<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">用户管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <br />
    <el-card>
      <!-- 搜索 -->
      <el-input placeholder="请输入内容" v-model="user.query"></el-input>
      <el-button @click="search">搜索</el-button>
      <el-button type="primary" @click="adduserDia">添加</el-button>
      <br />
      <br />
      <!-- 表格 -->
      <template>
        <el-table :data="users" border style="width: 100%">
          <el-table-column label="#" type="index"></el-table-column>
          <el-table-column label="姓名" prop="username"></el-table-column>
          <el-table-column label="邮箱" prop="email"></el-table-column>
          <el-table-column label="电话" prop="mobile"></el-table-column>
          <el-table-column label="角色" prop="role_name"></el-table-column>
          <el-table-column label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.mg_state"
                @change="zhuang(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="250px">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="bianji(scope.row)"
                >编辑</el-button
              >
              <el-button type="danger" size="mini" @click="del(scope.row.id)"
                >删除</el-button
              >
              <el-button
                type="warning"
                size="mini"
                @click="setRoleToUser(scope.row)"
                >设置</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
    </el-card>
    <br />
    <!-- 分页 -->
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="user.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <!-- 添加弹框 -->
    <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
      <el-form :model="adduser" :rules="rules" ref="addForm">
        <el-form-item
          label="用户名"
          prop="username"
          :label-width="formLabelWidth"
        >
          <el-input v-model="adduser.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="password"
          :label-width="formLabelWidth"
        >
          <el-input v-model="adduser.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
          <el-input v-model="adduser.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile" :label-width="formLabelWidth">
          <el-input v-model="adduser.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="tijiao">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 编辑的模态框 --> 
    <el-dialog title="修改用户" :visible.sync="dialogFormVisibles">
      <el-form :model="addusers" :rules="rules" ref="xgaddForms">
        <el-form-item
          label="用户名"
          prop="username"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="addusers.username"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email" :label-width="formLabelWidth">
          <el-input v-model="addusers.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile" :label-width="formLabelWidth">
          <el-input v-model="addusers.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibles = false">取 消</el-button>
        <el-button type="primary" @click="tjbianji">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 分配权限 -->
    <el-dialog title="分配权限" :visible.sync="dialogFormVisibless">
      <el-form :model="Roleform">
        <el-form-item label="当前用户" :label-width="formLabelWidth">
          <span prop="username"> {{ Roleform.username }}</span>
        </el-form-item>
        <el-form-item label="当前角色" :label-width="formLabelWidth">
          <span>{{ Roleform.role_name }}</span>
        </el-form-item>
        <el-form-item label="分配角色" >
      <el-select  placeholder="请选择角色" v-model="rid">
        <el-option   v-for="item in Roles" :key="item.id" :label="item.roleName" :value="item.id"></el-option>
      </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibless = false">取 消</el-button>
        <el-button type="primary" @click=" aaa"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import _ from "lodash";
import {
  getUserListapi,
  addUserApi,
  addUserApis,
  delApi,
  getzhuangApi,
  getjueseApi,
  getfenApl,
} from "@/http/api";
export default {
  data() {
    //邮箱验证规则
    const checkemail = (rule, value, callback) => {
      const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      if (!reg.test(value)) {
        return callback(new Error("请输入正确的邮箱"));
      }
      callback();
    };
    //验证手机号
    const checkmobile = (rule, value, callback) => {
      const regs = /^1[3456789]\d{9}$/;
      if (!regs.test(value)) {
        return callback(new Error("请输入正确手机号"));
      }
      callback();
    };
    return {
      formLabelWidth: "90px",
      adduser: {
        //添加用户的数据
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      addusers: {
        //修改用户的数据
        username: "",
        password: "",
        email: "",
        mobile: "",
        id: 0,
      },

      dialogVisible: false, //删除弹框
      dialogFormVisible: false, //添加的模态框
      dialogFormVisibles: false, //修改的模态框
      dialogFormVisibless: false, //权限的模态框
      users: [], //表格渲染的数据
      value: true, //校验
      total: 0,
      Roleform: {},   //分配权限的权限名字和角色名
      Roles:{},
      user: {
        query: "",
        pagenum: 1,
        pagesize: 4,
      },
      rid:'',
      //校验
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        email: [{ validator: checkemail, trigger: "blur" }],
        mobile: [{ validator: checkmobile, trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.getlist();
  },
  methods: {
    //提交分配角色
  async aaa(){ 
      console.log(this.rid,this.Roleform.id);
      await getfenApl(this.Roleform.id,this.rid) 
      this.dialogFormVisibless=false
      this.getlist() 
    },
    //防抖
    search: _.debounce(function () {
      this.getlist();
    }, 300),
    //节流
    search: _.throttle(function () {
      this.getlist();
    }, 3000),
    //添加
    adduserDia() {
      this.dialogFormVisible = true;
    },
    //提交
    tijiao() {
      //校验用户名 密码 邮箱 手机号
      this.$refs.addForm.validate(async (valid) => {
        if (!valid) {
          return false;
        }
        //提交给后台
        const res = await addUserApi(this.adduser);
        this.dialogFormVisible = false;
      });
    },
    //编辑
    bianji(row) {
      console.log(row);
      this.dialogFormVisibles = true;
      this.addusers = row;
    },
    //提交编辑
    tjbianji() {
      this.$refs.xgaddForms.validate(async (valid) => {
        if (!valid) {
          return false;
        }
        //提交给后台
        let { id, mobile, email } = this.addusers;
        let res = await addUserApis(id, { mobile, email });
        this.dialogFormVisibles = false;
      });
    },
    //删除
    del(id) {
      console.log(id);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await delApi(id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getlist();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //修改状态
    async zhuang(row) {
      console.log("row", row);
      let userid = row.id;
      let type = row.mg_state;
      await getzhuangApi(userid, type);
    },
    //打开用户分配角色对话框
    async setRoleToUser(user){ 
      console.log("user", user);
      this.Roleform = user;
      let res = await getjueseApi(); 
      console.log("角色列表", res);
      this.Roles=res
      console.log(this.Roles,'111')

      this.dialogFormVisibless = true;
    },
    handleSizeChange(val) {
      console.log("当前每页显示条数", val);
      this.user.pagesize = val;
      this.getlist();
    },
    handleCurrentChange(val) {
      console.log("当前页码", val);
      this.user.pagenum = val;
      this.getlist();
    },
    //表格的数据
    async getlist() {
      let res = await getUserListapi(this.user);
      console.log(this.user);
      let { total, users } = res;
      this.total = total;
      this.users = users;
    },
  },
};
</script>

<style scoped>
.el-input {
  width: 350px;
}
</style>
